<template>
  <div class="dialog-form">
    <!-- 添加或修改弹出框 -->
    <el-dialog
      :title="$t('batch.systemConfig.form.actuatorMan')"
      :visible.sync="open"
      @close="close()"
      width="800px"
      append-to-body
    >
      <div class="discountReceipt-pick-operate-box">

         <Ht-search-form
           :defaultEventKey="defaultEventKey"
           :dictMap="dictMap"
           :queryParams="queryParams"
           :outputQueryParams="queryParams"
           :formOptions="formOptions"
           :advOptions="[]"
          ></Ht-search-form>

        <div class="operate-box-bottom">
          <div class="bottom-content">
            <ht-table
              :defaultEventKey="defaultEventKey"
              ref="dataTable"
              :initData="false"
              :multiSelect="false"
              :dictMap="dictMap"
              :queryParams="queryParams"
              :tableBindColumns="tableBindColumns"
              :opButtonList="opButtonList"
              :reqConfig="reqConfig"
              :showRightToolbar="false"
            >
              <template v-slot:operate>
                <el-table-column
                  :label="$t('common.operate')"
                  align="center"
                  width="160"
                  class-name="small-padding fixed-width"
                >
                  <template slot-scope="scope">
                    <ht-button-group
                      :buttonList="[
                        {
                          label: $t('common.edit'),
                          permission: '',
                          doFunc: () => {
                            handleAction('update', scope.row);
                          },
                        },
                        {
                          label: $t('batch.systemConfig.form.parameter'),
                          permission: '',
                          doFunc: () => {
                            handleAction('params', scope.row);
                          },
                        },
                        {
                          label: $t('common.delete'),
                          permission: '',
                          doFunc: () => {
                            handleAction('del', scope.row);
                          },
                        },
                      ]"
                    />
                  </template>
                </el-table-column>
              </template>
            </ht-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">{{
          $t("common.close")
        }}</el-button>
      </div>
    </el-dialog>
    <edit-actuator ref="editActuator" @editComplete="editComplete" />
    <edit-actuator-params ref="editActuatorParams" />
  </div>
</template>

<script>
import { apiUrlConfig, del } from "@/api/batch_app/systemConfig/actuator";
import { listPageMixin } from "@/libs/listPageMixin";
import { dialogMixin } from "@/libs/dialogMixin";
import EditActuator from "@/views/batch/systemConfig/components/EditActuator";
import KvConfig from "@/views/batch/components/kvConfig";
import EditActuatorParams from "@/views/batch/systemConfig/components/EditActuatorParams";
export default {
  name: "ActuatorPage",
  mixins: [listPageMixin, dialogMixin],
  components: { EditActuatorParams, KvConfig, EditActuator },
  data() {
    return {
      defaultEventKey: "actuator",
      reqConfig: apiUrlConfig,
      sysConfId: "",
      queryParams: {
        sysConfId: "",
        executorName: ''
      },
      opButtonList: () => {
        return [
          {
            label: this.$t("common.add"),
            type: "primary",
            icon: "el-icon-plus",
            permission: "",
            actionType: "add",
            clickFun: () => {
              this.handleAction("add");
            },
          },
        ];
      },
      tableBindColumns: [
        { label: `ID`, prop: "id", visible: false },
        {
          label: this.$t("batch.systemConfig.form.actuatorName"),
          prop: "executorName",
        },
        {
          label: this.$t("batch.systemConfig.form.actuatorCode"),
          prop: "executorCode",
        },
        {
          label: this.$t("batch.systemConfig.form.actuatorType"),
          prop: "executorType",
          dictCode: "executor_type",
        },
        {
          label: this.$t("batch.systemConfig.form.channelShared"),
          prop: "channelShared",
          formatter: (row, value, index) => {
            if (value == 1) {
              return this.$t("common.yes");
            } else {
              return this.$t("common.no");
            }
          },
        },
      ],
    };
  },
  methods: {
    show(sysConfId) {
      if (sysConfId) {
        this.sysConfId = sysConfId;
        this.queryParams.sysConfId = sysConfId;
        this.$nextTick(() => {
          this.doQuery(this.defaultEventKey);
        });
      }
      this.open = true;

      const formOptions = [
        {
          type: "input",
          prop: "executorName",
          defaultAttr: {
            size: "small",
            label: this.$t("batch.systemConfig.form.actuatorName"),
            placeholder: this.$t("batch.systemConfig.form.actuatorName"),
          },
        },
      ];
      this.formOptions = formOptions;
    },
    handleAction(type, row = {}) {
      switch (type) {
        case "add":
          this.$refs.editActuator.show("", this.sysConfId, false);
          break;
        case "params":
          this.$refs.editActuatorParams.show(row.id, row.sysConfId);
          break;
        case "del":
          const id = row.id || this.ids.join(",");
          this.$confirm(
            this.$t("batch.systemConfig.rules.deleteActuator"),
            this.$t("common.warning"),
            {
              confirmButtonText: this.$t("common.confirm"),
              cancelButtonText: this.$t("common.cancel"),
              type: "warning",
            }
          )
            .then(function () {
              return del(id);
            })
            .then(() => {
              this.doQuery(this.defaultEventKey);
              this.msgSuccess(this.$t("common.deleteSuccess"));
            })
            .catch((action) => {});
          break;
        case "update":
          this.$refs.editActuator.show(row.id, this.sysConfId, true);
          break;
      }
    },
    editComplete(state) {
      if (state) {
        // 编辑完成，刷新列表数据
        this.doQuery(this.defaultEventKey);
      }
    },
  },
};
</script>

<style scoped>
</style>

